package java101;
import javax.swing.JOptionPane;

public class GreatCircleDistance {

	public static void main(String[] args) {
		
		try {
			//Receive input
			String latInput = JOptionPane.showInputDialog
			("Enter the degrees for the first point, separated by commas (10,30): ");
			String[] point1 = latInput.split(",");
			
			String lonInput = JOptionPane.showInputDialog
			("Enter the degrees for the second point, separated by commas (10,30): ");
			String[] point2 = lonInput.split(",");
			
			//Convert to radian double			
			double lat1 = Math.toRadians(Double.parseDouble(point1[0]));
			double lon1 = Math.toRadians(Double.parseDouble(point1[1]));
			double lat2 = Math.toRadians(Double.parseDouble(point2[0]));
			double lon2 = Math.toRadians(Double.parseDouble(point2[1]));
			
			//Perform equation in radians (degrees doesn't work)
			double arc = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(Math.abs(lon1-lon2)));

			//Multiply by 60 (number of miles per degree) - currently nautical miles
			double nauticalMiles = Math.toDegrees(arc) * 60;
			double kilometer = nauticalMiles * 1.852;
			
			//Format and print distance
			String nauticalFormat = String.format("%.2f", nauticalMiles);
			System.out.println(nauticalFormat + " nautical miles.");
			String kiloFormat = String.format("%.2f", kilometer);
			System.out.println(kiloFormat + " kilometers.");
			} 
		
		catch (NumberFormatException e) {
			System.out.println("You didn't enter two numbers separated by a comma! :'(");
			System.out.println("Use numbers next time.");
			}
		catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("You didn't enter real degree numbers! :'(");
			System.out.println("Enter true degrees next time.");
			}
		catch (NullPointerException e) {
			System.out.println("You pressed cancel! :'(");
			}
}
}